<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.


echo _("Adding new page to database, please wait ...");

include("../includes/sql.php");
include("../sites/${_GET['site']}/config/config.php");

if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="Please login with your username and password"');
    header('HTTP/1.0 401 Unauthorized');
    die('Please provide username and password!');
}
else {
    $sql_username = $_SERVER['PHP_AUTH_USER'];
    $sql_password = $_SERVER['PHP_AUTH_PW'];
}

// connect to db
try {
    $db = new SQL($sql_username, $sql_password, $sql_hostname, $sql_database);
}
catch(PDOException $e) {
    header('WWW-Authenticate: Basic realm="Wrong username/password\nPlease login with your username and password"');
    header('HTTP/1.0 401 Unauthorized');
    die('XPlease provide username and password!');
}

function page_get_next_id(){
    global $db;
    $r = $db->query( "SELECT * FROM pages" );
    $ids = array();
    foreach($r as $row){
        $ids[] = $row[id];
    }
    return max($ids)+1;
}

$url = $_GET["url"];
//echo "url: $url";

$title = _("New page");
$message = _("Write here the content");
$id = page_get_next_id();
$current_page = $id;
$db->exec( "INSERT INTO pages (id, title, content, lang) VALUES ($id, \"$title\", \"$msg\", \"en\") ");

//menu
$db->exec( "INSERT INTO menu (position, title, id, lang) VALUES (0, \"$title\", $id, \"en\") ");

$url = "../index.php{$url}action=edit&page=$id";
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$url\">";
die();
?>
